﻿/*******************************************************************************
 * CreateTime:2022-04-04 11:37:47
 * Description: YoursLC有源低代码
*********************************************************************************/

using System;
using System.Data;
using System.Collections.Generic;
using Think9.Models;
using Think9.Services.Base;
using Think9.Services.Basic;
using Think9.Services.Table;
using Think9.Services.Flow;

namespace Think9.Services.CodeBuild
{
	/// <summary>
	/// 主表Service
	/// </summary>
	public class Demo01VariousControlsService : BaseService<Demo01VariousControlsModel>
	{
		private ComService ComService = new ComService();
		private string split = BaseUtil.ComSplit;//字符分割 用于多选项的分割等 如split+选项1+split+选项2+split
		private string sql;

		//添加前数据处理
		public int BeforeAdd(ref string err, CurrentUserEntity CurrentUser, CurrentPrcsEntity mPrcs, string flowid, string maintbid, string tbname)
		{
			int listid = 0;

			//处理自动编号-- 如果有则添加一条数据,返回自增长的listid;如果没有则返回0
			listid = AutoNo.SetAutoNumber(ref err, CurrentUser, mPrcs, flowid, tbname);



			return listid;
		}

		//编辑后数据处理
		public string AfterEdit(string listid, string flowid, string prcsid, CurrentUserEntity user)
        {
			string err = "";

			//调用通用函数实现数据回写，用户可自定义 数据回写在录入表管理/数据读写中设置
			WriteBack WriteBack = new WriteBack();
			string maintbid = flowid.Replace("bi_", "tb_").Replace("fw_", "tb_");
			err = WriteBack.WriteValueByRelationSet(listid, flowid, maintbid, prcsid, user);

			return err;
		}

		//完成后数据处理
		public string AfterFinish(string listid, string flowid, string prcsid, CurrentUserEntity user)
		{
			string err = AfterEdit(listid, flowid, prcsid, user);
			if(string.IsNullOrEmpty(err))
            {
				//数据锁定等
				FlowCom.FinishFlowPrcs(listid, flowid, prcsid, user);
            }

			return err;
		}

		/// <summary>
		/// 主表查询调用 返回列表 查询条件可在录入表指标属性中设置
		/// </summary>
		/// <param name="total"></param>
		/// <param name="model"></param>
		/// <param name="pageInfo"></param>
		/// <param name="user"></param>
		/// <param name="flowid"></param>
		/// <param name="tbid"></param>
		/// <returns></returns>
		public IEnumerable<dynamic> GetPageListBySearch(ref long total, Demo01VariousControlsModel model, PageInfoEntity pageInfo, CurrentUserEntity user, string flowid, string tbid)
		{
			pageInfo.returnFields = "FlowRunList.isLock,FlowRunList.BeginTime, FlowRunList.BeginUser," + tbid + ".* ";
			string where = BasicHelp.GetWhereByFlowId(user, flowid);
			string some = "  FlowRunList INNER JOIN " + tbid + " ON FlowRunList.listid = " + tbid + ".listid ";

			//自动编号
			if (!string.IsNullOrEmpty(model.inAutoNumber))
			{
				where += " and (tb_Demo01VariousControls.inAutoNumber like @inAutoNumber)"; 
				model.inAutoNumber = string.Format("%{0}%", model.inAutoNumber); 
			}
			//输入文本框1
			if (!string.IsNullOrEmpty(model.inEnterTextBox1))
			{
				where += " and (tb_Demo01VariousControls.inEnterTextBox1 like @inEnterTextBox1)"; 
				model.inEnterTextBox1 = string.Format("%{0}%", model.inEnterTextBox1); 
			}
			//输入文本框2
			if (!string.IsNullOrEmpty(model.inEnterTextBox2))
			{
				where += " and (tb_Demo01VariousControls.inEnterTextBox2 like @inEnterTextBox2)"; 
				model.inEnterTextBox2 = string.Format("%{0}%", model.inEnterTextBox2); 
			}
			//单选
			if (!string.IsNullOrEmpty(model.inSingleChoice))
			{
				where += " and (tb_Demo01VariousControls.inSingleChoice = @inSingleChoice)"; 
			}
			//日期选择
			if(model.inDateSelection != null)
			{
				where += " and (tb_Demo01VariousControls.inDateSelection >= @inDateSelection)"; 
			}
			if(model.inDateSelection_Exa  != null)
			{
				where += " and (tb_Demo01VariousControls.inDateSelection <= @inDateSelection_Exa)"; 
			}
			//下拉选择
			if (!string.IsNullOrEmpty(model.inDropDownSelection))
			{
				where += " and (tb_Demo01VariousControls.inDropDownSelection = @inDropDownSelection)"; 
			}
			//树形选择 下拉选择树
			if (!string.IsNullOrEmpty(model.inDropDownSelectionTree))
			{
				where += " and (LEFT(tb_Demo01VariousControls.inDropDownSelectionTree,"+ model.inDropDownSelectionTree.Length+ ") = @inDropDownSelectionTree)"; 
			}
			//文本框弹出1
			if (!string.IsNullOrEmpty(model.inTextBoxPopup1))
			{
				where += " and (tb_Demo01VariousControls.inTextBoxPopup1 like @inTextBoxPopup1)"; 
				model.inTextBoxPopup1 = string.Format("%{0}%", model.inTextBoxPopup1); 
			}
			//文本框弹出2
			if (!string.IsNullOrEmpty(model.inTextBoxPopup2))
			{
				where += " and (tb_Demo01VariousControls.inTextBoxPopup2 like @inTextBoxPopup2)"; 
				model.inTextBoxPopup2 = string.Format("%{0}%", model.inTextBoxPopup2); 
			}
			//日期加时间
			if(model.inDatePlusTime != null)
			{
				where += " and (tb_Demo01VariousControls.inDatePlusTime >= @inDatePlusTime)"; 
			}
			if(model.inDatePlusTime_Exa  != null)
			{
				where += " and (tb_Demo01VariousControls.inDatePlusTime <= @inDatePlusTime_Exa)"; 
			}
			//多选
			if (!string.IsNullOrEmpty(model.inMultipleChoice))
			{
				where += " and (tb_Demo01VariousControls.inMultipleChoice like @inMultipleChoice)"; 
				model.inMultipleChoice = string.Format("%{0}%",split + model.inMultipleChoice + split); 
			}
			//图片
			if (!string.IsNullOrEmpty(model.inPicture))
			{
				where += " and (tb_Demo01VariousControls.inPicture like @inPicture)"; 
				model.inPicture = string.Format("%{0}%", model.inPicture); 
			}
			return base.GetPageUnite(ref total, model, pageInfo, some + where);

		}

		/// <summary>
		/// 主表 为下拉选择、多选、单选准备动态数据源
		/// 负责列表显示时value与text转化 动态数据源来源于为指标指定的数据规范
		/// </summary>
		/// <param name="from"></param>
		/// <param name="param"></param>
		/// <returns></returns>
		public IEnumerable<valueTextEntity> GetSelectList(string from, object param)
		{
			DataTable dt = DataTableHelp.NewValueTextDt();

			//单选
			sql = "select DetailName as id,DetailName as name FROM sys_itemsdetail where ItemCode = 'RawMaterialsOrProducts' ORDER BY OrderNo";
			foreach (DataRow dr in ComService.GetDataTable(sql, param).Rows)
			{
				DataRow row = dt.NewRow();
				row["ClassID"] = "inSingleChoice";
				row["Value"] = dr["id"].ToString();
				row["Text"] = dr["name"].ToString();
				dt.Rows.Add(row);
			}
			//下拉选择
			sql = "select inCode as id,inName as name FROM tb_Data04 ORDER BY inCategory";
			foreach (DataRow dr in ComService.GetDataTable(sql, param).Rows)
			{
				DataRow row = dt.NewRow();
				row["ClassID"] = "inDropDownSelection";
				row["Value"] = dr["id"].ToString();
				row["Text"] = dr["name"].ToString();
				dt.Rows.Add(row);
			}
			//树 下拉选择树
			sql = "select inCode as id,inName as name FROM tb_Data05 ORDER BY LENGTH(inCode)  ,inCode";
			BaseUtil.GetTreeDt(dt, ComService.GetDataTable(sql),"inDropDownSelectionTree");
			//多选
			sql = "select DetailName as id,DetailName as name FROM sys_itemsdetail where ItemCode = 'Company' ORDER BY OrderNo";
			foreach (DataRow dr in ComService.GetDataTable(sql, param).Rows)
			{
				DataRow row = dt.NewRow();
				row["ClassID"] = "inMultipleChoice";
				row["Value"] = dr["id"].ToString();
				row["Text"] = dr["name"].ToString();
				dt.Rows.Add(row);
			}

			//DataTable转换成IEnumerable
			return DataTableHelp.ToEnumerable<valueTextEntity>(dt);
		}

		/// <summary>
		/// 弹出页面的查询条件有下拉选择、多选、单选时，为其绑定动态数据源
		/// 动态数据源来源于为指标指定的数据规范
		/// </summary>
		/// <param name="tbid"></param>
		/// <param name="indexid"></param>
		/// <param name="from"></param>
		/// <param name="param"></param>
		/// <returns></returns>
		public IEnumerable<valueTextEntity> GetSelectList(string tbid, string indexid, string from, object param)
		{
			DataTable dt = DataTableHelp.NewValueTextDt();

			//主表指标弹出页面
			if (tbid == "_main")
			{
				if (indexid == "inTextBoxPopup1")
				{
					sql = "select DetailName as id,DetailName as name FROM sys_itemsdetail where ItemCode = 'Department' ORDER BY OrderNo";
					foreach (DataRow dr in ComService.GetDataTable(sql).Rows)
					{
						DataRow row = dt.NewRow();
						row["ClassID"] = "inEmployeeDepartment";
						row["Value"] = dr["id"].ToString();
						row["Text"] = dr["name"].ToString();
						dt.Rows.Add(row);
					}
					sql = "select DetailName as id,DetailName as name FROM sys_itemsdetail where ItemCode = 'Post' ORDER BY OrderNo";
					foreach (DataRow dr in ComService.GetDataTable(sql).Rows)
					{
						DataRow row = dt.NewRow();
						row["ClassID"] = "inEmployeePosition";
						row["Value"] = dr["id"].ToString();
						row["Text"] = dr["name"].ToString();
						dt.Rows.Add(row);
					}
				}
				if (indexid == "inTextBoxPopup2")
				{
					sql = "select DetailName as id,DetailName as name FROM sys_itemsdetail where ItemCode = 'Company' ORDER BY OrderNo";
					foreach (DataRow dr in ComService.GetDataTable(sql).Rows)
					{
						DataRow row = dt.NewRow();
						row["ClassID"] = "inName2";
						row["Value"] = dr["id"].ToString();
						row["Text"] = dr["name"].ToString();
						dt.Rows.Add(row);
					}
				}
			}
			else//子表指标弹出页面
			{
				if (indexid == "SubTable01v4")
				{
					sql = "select DetailName as id,DetailName as name FROM sys_itemsdetail where ItemCode = 'Department' ORDER BY OrderNo";
					foreach (DataRow dr in ComService.GetDataTable(sql).Rows)
					{
						DataRow row = dt.NewRow();
						row["ClassID"] = "inEmployeeDepartment";
						row["Value"] = dr["id"].ToString();
						row["Text"] = dr["name"].ToString();
						dt.Rows.Add(row);
					}
					sql = "select DetailName as id,DetailName as name FROM sys_itemsdetail where ItemCode = 'Post' ORDER BY OrderNo";
					foreach (DataRow dr in ComService.GetDataTable(sql).Rows)
					{
						DataRow row = dt.NewRow();
						row["ClassID"] = "inEmployeePosition";
						row["Value"] = dr["id"].ToString();
						row["Text"] = dr["name"].ToString();
						dt.Rows.Add(row);
					}
				}
			}

			//DataTable转换成IEnumerable
			return DataTableHelp.ToEnumerable<valueTextEntity>(dt);
		}

		/// <summary>
		/// 数据删除
		/// </summary>
		/// <param name="id"></param>
		/// <param name="fwid"></param>
		/// <returns></returns>
		public bool DeleteByID(string id, string fwid)
		{
			//删除子表数据
			ComService.ExecuteSql("delete from tb_SubTable01 where ListId=" + id + "");
			AttachmentService.DelAttachment(int.Parse(id), fwid);//删除附件 要最先执行
			//删除flowrunlist、flowrunprcslist中关联数据
			if (fwid.StartsWith("fw_"))
			{
				ComService.ExecuteSql("delete from flowrunlist where ListId = " + id + "");
				ComService.ExecuteSql("delete from flowrunprcslist where ListId = " + id + "");
			}

			AutoNo.DelAutoNo(id, fwid);//删除自动编号

			return base.DeleteByWhere("where ListId=" + id + "");//删除主表数据
		}

		/// <summary>
		///返回可修改字段 自由流程排除了隐藏指标 固定流程排除了隐藏指标及不可写字段
		/// </summary>
		/// <param name="prcno"></param>
		/// <returns></returns>
		public string GetUpdateFields(string prcno)
		{
			//自由流程或无流程排除了隐藏指标
			 return "inAutoNumber,inEnterTextBox1,inEnterTextBox2,inSingleChoice,inDateSelection,inDropDownSelection,inDropDownSelectionTree,inTextBoxPopup1,inTextBoxPopup2,inDatePlusTime,inMultipleChoice,inPicture";
		}


	}
}